﻿using Blm.Utils.Extensions;
using System;
using System.Linq;
using System.Threading.Tasks;
using VisionCloud.Core.Model.Sys;
using VisionCloud.Domain.DtoModel.Sys;
using VisionCloud.Domain.Repository.Interface.Sys;

namespace VisionCloud.Domain.Repository.Implement.Sys
{
    public class SysDeviceUserRepository : BaseRepository<SysDeviceUser>, ISysDeviceUserRepository
    {
        /// <summary>
        /// 查询用户设备注册信息
        /// </summary>
        /// <returns></returns>
        public async Task<SysDeviceUserDto> GetSysDeviceUser(string usbKeyNO, Guid yumUserGuid)
        {
            var result = (from device in Db.SysDevices
                          join deviceUser in Db.SysDeviceUsers on device.DeviceGuid equals deviceUser.DeviceGuid
                          where device.EncryptedDogNumber == usbKeyNO && deviceUser.UserGuid == yumUserGuid
                          select new SysDeviceUserDto()
                          {
                              DeviceGuid = device.DeviceGuid,
                              YunUserGuid = deviceUser.UserGuid,
                              IsActive = device.IsActive,
                              EncryptedDogNumber = device.EncryptedDogNumber,
                              EncryptedDogType = device.EncryptedDogType
                          }).FirstOrDefault();

            return result;
        }

        /// <summary>
        /// 查询用户设备注册信息
        /// </summary>
        /// <returns></returns>
        public async Task<SysDeviceUserDto> GetSysDeviceUserByEmail(string usbKeyNO, string email)
        {
            var result = (from device in Db.SysDevices
                          join deviceUser in Db.SysDeviceUsers on device.DeviceGuid equals deviceUser.DeviceGuid
                          join sysUser in Db.SysUsers on deviceUser.UserGuid equals sysUser.YunUserGuid
                          where device.EncryptedDogNumber == usbKeyNO && sysUser.Email == email
                          select new SysDeviceUserDto()
                          {
                              DeviceGuid = device.DeviceGuid,
                              YunUserGuid = deviceUser.UserGuid,
                              IsActive = device.IsActive,
                              EncryptedDogNumber = device.EncryptedDogNumber,
                              EncryptedDogType = device.EncryptedDogType
                          }).FirstOrDefault();

            return result;
        }

        /// <summary>
        /// 查询用户设备注册信息
        /// </summary>
        /// <returns></returns>
        public async Task<SysDeviceUserDto> GetSysDeviceUserByUserID(string usbKeyNO, string userID)
        {
            var result = (from device in Db.SysDevices
                          join deviceUser in Db.SysDeviceUsers on device.DeviceGuid equals deviceUser.DeviceGuid
                          join sysUser in Db.SysUsers on deviceUser.UserGuid equals sysUser.YunUserGuid
                          where device.EncryptedDogNumber == usbKeyNO && sysUser.YunUserGuid == userID.ToGuid()
                          select new SysDeviceUserDto()
                          {
                              DeviceGuid = device.DeviceGuid,
                              YunUserGuid = deviceUser.UserGuid,
                              IsActive = device.IsActive,
                              EncryptedDogNumber = device.EncryptedDogNumber,
                              EncryptedDogType = device.EncryptedDogType
                          }).FirstOrDefault();

            return result;
        }

        /// <summary>
        /// 通过账号，加密狗类型，加密狗编号判断用户和设备是否已经绑定
        /// </summary>
        /// <param name="userAccount">用户账号</param>
        /// <param name="encryptedDogNumber"></param>
        /// <param name="encryptedDogType"></param>
        /// <returns></returns>
        public bool IsUserBoundDevice(string userAccount, string encryptedDogNumber, string encryptedDogType)
        {
            return (from device in Db.SysDevices
                    join deviceUser in Db.SysDeviceUsers on device.DeviceGuid equals deviceUser.DeviceGuid
                    join user in Db.SysUsers on deviceUser.UserGuid equals user.YunUserGuid
                    where
                    device.EncryptedDogNumber == encryptedDogNumber
                    && device.EncryptedDogType == encryptedDogType
                    && user.AccountNumber == userAccount
                    select deviceUser
                        ).Any();
        }

        /// <summary>
        ///  设备是否已经绑定给用户
        /// </summary>
        /// <param name="encryptedDogNumber"></param>
        /// <param name="encryptedDogType"></param>
        /// <returns></returns>
        public bool IsDeviceBound(string encryptedDogNumber, string encryptedDogType)
        {
            return (from device in Db.SysDevices
                    join deviceUser in Db.SysDeviceUsers on device.DeviceGuid equals deviceUser.DeviceGuid
                    where
                    device.EncryptedDogNumber == encryptedDogNumber
                    && device.EncryptedDogType == encryptedDogType
                    select deviceUser
                        ).Any();
        }

        public void BoundDeviceToUse(Guid AccountGuid, Guid deviceGuid)
        {
            Db.SysDeviceUsers.Add(new SysDeviceUser()
            {
                DeviceGuid = deviceGuid,
                UserGuid = AccountGuid,
                CreatedBy = AccountGuid,
                CreatedDate = DateTime.Now
            });
        }
    }
}